
FIUMAPS.LocationsByName = {intLoaded:0, curLetter: "", hasServicesHeader: false, hasBuildingsHeader: false, services_ul: null, buildings_ul: null,
    LoadLetter: function (letter) {
        // if letter is not already loaded, load.
        if(this.curLetter !== letter){
            //show modal window
            FIUMAPS.ModalWindow.show($('MapLoading'));
            this.assignNewLetter(letter);
            
            ClearOverlays();
            $('LocationsByLetter').innerHTML = "";// clear
            //reset counter
            this.intLoaded = 0;
            if(FIUMAPS.Cache.isLetterLoaded(this.curLetter)){
            //already in cache. pull from it.
                this.LoadLetterListFromCache();
            }else{
                this.loadBuildings();
                this.loadServices();
                // register that this letter has been loaded into cache.  
                FIUMAPS.Cache.setLetterLoaded(this.curLetter) 
            }       
        }
    },
    Reset: function () {
        $('LocationsByLetter').innerHTML = "";// clear
        if(this.curLetter){
            $('letterJump_' + this.curLetter).removeClass('active');
        }
        this.curLetter = ""; 
    },    
    LoadLetterListFromCache: function () {
        
        this.hasServicesHeader = false;
        this.hasBuildingsHeader = false; 
        
        var arrLocationCache = FIUMAPS.Cache.Data[FIUMAPS.CampusInfo.getCurrentCampusCode()].Locations;
       
        //loop through cache looking for letter.
        for (var i = 0; i < arrLocationCache.length; i++) {
//console.log(arrLocationCache[i]);
            if(arrLocationCache[i].Letter == this.curLetter){ // found letter.
                this.createHTMLList(arrLocationCache[i].Geometries,arrLocationCache[i].Attributes);
            }   
        }
        //hide modal window.
        FIUMAPS.ModalWindow.hide();
        accordionResize(current_tab);
    },
    assignNewLetter: function (letter) {
        // change css class
        var curLetterID = "letterJump_" + this.curLetter;
        if(this.curLetter){
            $('letterJump_' + this.curLetter).removeClass('active');
        }
        $('letterJump_' + letter).addClass('active');  
        //assign member variable for letter
        this.curLetter = letter.toUpperCase();
    },   
    createHTMLList: function (geometry,attributes) {
        // create html list item element
        var li = document.createElement('li');
        // create html link item element
        var link = this.CreateLinkElement(attributes.NAME);

        if(attributes.SERVICE_ID){
            // Services/GMarker
            //add hover rules to all geometries associated to this feature.
            addPointListeners(geometry,attributes,link);
        }else{
            //Buildings/ GPolygon
            //add hover rules to all geometries associated to this feature.         
            addPolygonListeners(geometry,attributes,link);        
        }   
                                
        // add link to list item container
        li.appendChild(link);
        
        this.addToList(attributes,li);
    },
    addToList: function (attributes,li) {
        // add this item to HTML list.
        if(attributes.SERVICE_ID){//item is a service
            if(!this.hasServicesHeader){
                //Services header does not exist in list, create it.
                this.services_ul = document.createElement('ul');
                this.CreateHeader("Services",this.services_ul);
                this.hasServicesHeader = true;         
            }        
            this.services_ul.appendChild(li); 
        }else{//item is a building
            if(!this.hasBuildingsHeader){
                //Services header does not exist in list, create it.
                this.buildings_ul = document.createElement('ul');
                this.CreateHeader("Buildings",this.buildings_ul);
                this.hasBuildingsHeader = true;         
            }          
            this.buildings_ul.appendChild(li);        
        }
    },
    CreateHeader: function (label,htmlElement) {
        var h4 = document.createElement('h4');
        h4.appendChild(document.createTextNode(label));
        $('LocationsByLetter').appendChild(h4);
        $('LocationsByLetter').appendChild(htmlElement); 
    },
    CreateLinkElement: function (label) {
        // create html link element 
        var link = document.createElement('a');
        link.style.cursor = 'pointer';
        link.style.marginBottom = '5px';
        link.title = label;
        link.appendChild(document.createTextNode(label));
        return link;  
    },  
    loadServices: function () {
        // Query Task
        var qtask = new esri.arcgis.gmaps.QueryTask("http://maps.fiu.edu/arcgis/rest/services/FIUCampusMaps/ServicesNBuildings/MapServer/0");
        // Query
        var query = new esri.arcgis.gmaps.Query();
        // set query parameters. 
        query.returnGeometry = true;
        query.where = "NAME LIKE '" + this.curLetter + "%' AND CAMPUS_ID=" + FIUMAPS.CampusInfo.getCurrentCampusID();
        query.outFields = ["OBJECTID","SERVICE_ID","NAME","DESCRIPTION","FLOOR","ROOM_NUMBER","CATEGORY_NAME","BUILDING_CODE","BUILDING_NAME","BUILDING_ID","CAMPUS_ID","CAMPUS_CODE","CAMPUS_NAME"];
        // execute query task
        qtask.execute(query, false, FIUMAPS.LocationsByName._Callback);
    },   
    loadBuildings: function () {
        // Query Task
        var qtask = new esri.arcgis.gmaps.QueryTask("http://maps.fiu.edu/arcgis/rest/services/FIUCampusMaps/ServicesNBuildings/MapServer/1");
        // Query
        var query = new esri.arcgis.gmaps.Query();
        // set query parameters. 
        query.returnGeometry = true;
        query.where = "NAME LIKE '" + this.curLetter + "%' AND CAMPUS_ID=" + FIUMAPS.CampusInfo.getCurrentCampusID();
        query.outFields = ["BUILDING_ID","NAME","CODE","DESCRIPTION","CAMPUS_ID","CAMPUS_CODE","CAMPUS_NAME"];
        // execute query task
        qtask.execute(query, false, FIUMAPS.LocationsByName._Callback); 


    },       
    LoadAlphabet: function () {
        var arrAlpha = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"];
        var ol = document.createElement('ol');
        //loop through all alphabet letters creating HTML elements for them on the page.
        for (var i = 0; i < arrAlpha.length; i++) {
            var li = document.createElement('li');
            li.id = "letterJump_" + arrAlpha[i].toString();
            var href = document.createElement('a');
            href.onclick=Function("LoadLetter('" + arrAlpha[i] + "')");
            href.appendChild(document.createTextNode(arrAlpha[i]));
            li.appendChild(href);
            ol.appendChild(li);
        }
        
        $('letterJump').appendChild(ol);
    },
    _Callback: function(fset){
        if(fset !== null){
            var results = fset.features, attributes, geometry;  
            //loop through features  
            for (var i = 0; i < results.length; i++) {
                attributes = results[i].attributes;
//console.log(attributes.NAME);
                geometry = results[i].geometry;
                //find out what type of location item it is
                var type = (attributes.SERVICE_ID) ? FIUMAPS.LocationTypes.SERVICE : FIUMAPS.LocationTypes.BUILDING;
                var letter = attributes.NAME.capitalize().substr(0,1);
                var item = new FIUMAPS.Cache.LocationCacheItem(type,letter,attributes,geometry);
                //add to cache.
                FIUMAPS.Cache.AddLocationCacheItem(item);
            } 
            
        }    
        
        FIUMAPS.LocationsByName.intLoaded += 1;
        if(FIUMAPS.LocationsByName.intLoaded == 2){
            FIUMAPS.LocationsByName.LoadLetterListFromCache();
        }
    }                
}